package com.longdaji.decoration.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.longdaji.decoration.R;
import com.longdaji.decoration.bean.Orders;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Kanade on 2018/3/31 0031.
 */

public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ProductHolder> {
    private List<Orders.OrdersBean.OrderBean.ProductsBean> mProductList = new ArrayList<>();
    private Context mContext;

    public ProductAdapter(List<Orders.OrdersBean.OrderBean.ProductsBean> productList) {
        this.mProductList = productList;
    }

    class ProductHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_commodity_picture)
        ImageView ivCommodityPicture;
        @BindView(R.id.tv_commodity_name)
        TextView tvCommodityName;
        @BindView(R.id.tv_commodity_attr)
        TextView tvCommodityAttr;
        @BindView(R.id.tv_commodity_del_flag)
        TextView tvCommodityDelFlag;
        @BindView(R.id.tv_commodity_price)
        TextView tvCommodityPrice;
        @BindView(R.id.tv_commodity_num)
        TextView tvCommodityNum;

        public ProductHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }

    @Override
    public ProductHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mContext == null) {
            mContext = parent.getContext();
        }
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_product, parent, false);
        ProductHolder holder = new ProductHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ProductHolder holder, int position) {
        Orders.OrdersBean.OrderBean.ProductsBean product = mProductList.get(position);
        String commodityImg = product.getGoodsSkuImg();
        String commodityName = product.getGoodsName();
        String commodityAttr = product.getGoodsAttr();
        String commodityDelFlag = product.getDelFlag();
        int commodityPrice = product.getGoodsPrice();
        int commodityNum = product.getProductNum();
        if (commodityImg != null) {
            Glide.with(mContext).load(product.getGoodsSkuImg()).into(holder.ivCommodityPicture);
        }
        if (commodityName != null) {
            holder.tvCommodityName.setText(commodityName);
        }
        if (commodityAttr != null) {
            holder.tvCommodityAttr.setText(commodityAttr);
        }
        if (commodityPrice != 0) {
            holder.tvCommodityPrice.setText("￥"+String.valueOf(commodityPrice));
        }
        if (commodityNum != 0) {
            holder.tvCommodityNum.setText("x" + String.valueOf(commodityNum));
        }

//        holder.tvCommodityName.setText(product.getGoodsName().toString());
//        holder.tvCommodityAttr.setText(product.getGoodsAttr().toString());
//        holder.tvCommodityDelFlag.setText(product.getDelFlag().toString());
//        holder.tvCommodityPrice.setText(String.valueOf(product.getGoodsPrice()));
//        holder.tvCommodityNum.setText(String.valueOf(product.getProductNum()));

    }

    @Override
    public int getItemCount() {
        return mProductList.size();
    }

    public void setData(List<Orders.OrdersBean.OrderBean.ProductsBean> list) {
        this.mProductList.clear();
        this.mProductList.addAll(list);
        notifyDataSetChanged();
    }

    private OnItemClickListener mOnItemClickListener;

    public interface OnItemClickListener {
        void onApplyRefundClick(int goodsId);

        void onEvaluateClick(int goodsId);

    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }


}
